let express = require('express')
let bodyParser = require('body-parser')

let app = express()

//配置body-parser
// 只要加上这个配置，则在req请求对象上会多出来一个属性：body
// 因此可以直接通过req.body来获取表单post请求体数据
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());


app.get('/server',function (request,response) {
    response.setHeader('Access-Control-Allow-Origin','*')
    let result = {
        name: request.query.name,
        status: request.query.status
    }
    let resultStr = JSON.stringify(result)
    response.send(resultStr)
})

app.post('/server',function (request,response){
    //设置响应头  设置允许跨域
    response.setHeader('Access-Control-Allow-Origin', '*');
    //响应头
    response.setHeader('Access-Control-Allow-Headers', '*');
    let result = {
        name: request.body.name,
        status: request.body.status
    }
    let resultStr = JSON.stringify(result)
    response.send(resultStr)
})

app.listen('8000',function () {
    console.log('启动中')
})